<?php
class MflightController extends AppController
{
	var $name = 'Mflight';
	// 使用了Quence,User,Flight,BookInfor,Plane五个Model
	var $uses = array('Quene' , 'User' , 'Flight' , 'BookInfo' , 'Plane','Airport');
	//beforeFilter 页面执行前执行
	//afterFilter 页面执行后执行
	//beforeRender 页面显示前执行
	//afterRender 页面显示后执行
	
	//显示空白页面
	function index(){}
	
	//获取所有航班信息
	function getAll()
	{
		$flight = $this->Flight->find('all');
		return $flight ;
	}
	
	function getPlaneAll()
	{
		$planes = $this->Plane->find('all');
		$array = array();
		
		$ix = 0;
		while( $planes[$ix] )
		{
			$array[$ix] = $planes[$ix]['Plane'];
			$ix ++;
		}
		
		return $array;
	}
	
	function getPlaneAll0()
	{
		//$aaa = $this->Airport->find('list',array('field'=>array('Airport.name')));
	    //return $aaa;
		$result = $this->Plane->find('all');
		$plane = array();
		$temp = array();
		$ix = 0;
		while( $result[$ix] != null )
		{
			$temp['city'] = $result[$ix]['Plane']['id'];
			$temp['id'] = $result[$ix]['Plane']['id'];
			array_push($plane,$temp);
			$ix ++;
		}
		return $plane;
	}
	function getPlaneAll1()
	{
		//$aaa = $this->Airport->find('list',array('field'=>array('Airport.name')));
	    //return $aaa;
		$result = $this->Plane->find('all');
		$plane = array();
		$temp = array();
		$ix = 0;
		while( $result[$ix] != null )
		{
			$temp['city'] = $result[$ix]['Plane']['id'];
			$temp['id'] = $result[$ix]['Plane']['id'];
			array_push($plane,$temp);
			$ix ++;
		}
		return $plane;
	}
	
	//获取飞机信息 输入：飞机ID号 输出 ：飞机信息
	function  getPlaneInfo( $id = NULL )
	{		
		//如果有传入ID参数， 输出此飞机的信息； 否则，报错。
		if (!empty ($id))
		{
			$plane = $this->Plane->find('first', 
			array('conditions' => 
				  array('Plane.id' => $id)));
			return $plane['Plane'];
		}
		else 
		{
			return false;
		}
		
	}
	
	//添加航班 
	//输入：航班号，时间，飞机型号，起飞地点，目的地，起飞时间，到达时间，
	//输入：头等舱座位，公务舱座位，经济舱座位，已定座位数
	//输出：添加成功或者失败原因
	function addFlight($flight,$time,$plane,$start,$end,$start_time,$end_time,
						$first_class=NULL,$fprice=0,
					    $second_class=NULL,$sprice=0,
						$third_class=NULL,$tprice=0,$booked=NULL)
	{
		$flight= array ('Flight' =>
						 array('plane'=>$plane,
							   'flight'=>$flight,
							   'date'=>$time,							 
							   'start'=>$start,
							   'end'=>$end,
							   'start_time'=>$start_time,
							   'end_time'=>$end_time,
							   'first_class'=>$first_class,
							   'fprice'=>$fprice,
							   'second_class'=>$second_class,
							   'sprice'=>$sprice,
							   'third_class'=>$third_class,
							   'tprice'=>$tprice,
							   'booked'=>$booked
							   )
						 );
		if ($this->Flight->save($flight,false))
		{
			return true ;
		}
		else
		{
			return false ;
		}
	}

	
	//删除航班 输入: 航班ID 输出：删除成功或者失败原因
	function delFlight($flight)
	{
		$flights=$this->Flight->find('first',array('conditions'=>array('flight'=>$flight)));
		$id=$flights['Flight']['id'];
		
		if($this->Flight->del($id,false))
		{
			return ture;
		}
		else
		{
			return false ;
		}
	}
	
	//根据航班号修改航班信息
	//输入： 航班号，时间，飞机，出发地，目的地，出发时间， 到达时间
    //输入： 头等舱座位信息，公务舱座位信息，经济舱座位信息，已定座位数
	//输出： 添加成功或者失败原因
	function modifyFlight( $flight, $time, $plane, $start, $end, $start_time,
						  $end_time,$first_class, $fprice,$second_class, $sprice,$third_class, $tprice,$booked)
	{
		if(!empty($flight))
		{
			$flights = $this->Flight->find('first',array('conditions' => array( 'Flight.flight'=>$flight)));
			$id = $flights['Flight']['id'];
			$flight = array ('Flight' =>
						 array('flight'=>$flight,
							   'date'=>$time,
							   'plane'=>$plane,
							   'start'=>$start,
							   'end'=>$end,
							   'start_time'=>$start_time,
							   'end_time'=>$end_time,
							   'first_class'=>$first_class,
							   'fprice'=>$fprice,
							   'second_class'=>$second_class,
							   'sprice'=>$sprice,
							   'third_class'=>$third_class,
							   'tprice'=>$tprice,
							   'booked'=>$booked
							   )
						 );
			$this->Flight->id=$id;
			if ($this->Flight->save($flight,false))
			{
				return true ;
			}
			else
			{
				return false ;
			}
		}
		else
		{
			return 'false1';
		}
	}

	
	//获取已定座位信息
	//输入：航班ID
	//输出:以数组形式输出座位，否则输出错误
	function getBookedseat($id)  
	//通过航班ID获取flight中Booked的值
	{
		$this->Flight->id = $id;
		$booked=$this->Flight->field('booked');
		$booked1=explode(',',$booked);
		if(!empty($booked1))
		{
			return $booked1;
		}
		else
		{
			return false;
		};
	}
	
	//获取飞机所有座位信息
	//输入：航班ID
	//输出：以数组形式输出座位，否则输出错误
	function getAllseat($id)
	//通过航班ID获取plane的值，传递到model‘Plane’中
	//通过访问'Plane',获取其头等舱座位情况，公务舱座位情况，经济舱座位情况存储于3个数组中.然后将3个一维数组合并成三维数组
	//输出所对应航班ID的飞机所有座位的信息
	{
		$this->Flight->id = $id;
		$planeID= (int)$this->Flight->field('plane');

		 $plane = $this->Plane->find('all', 
			array(
			    'conditions' => array
					(
						'Plane.id' => $planeID
					)
				)
			);

		$booked1 = $plane[0]['Plane']['first_info'];
		$booked2 = $plane[0]['Plane']['second_info'];
		$booked3 = $plane[0]['Plane']['third_info'];
		
		$first_info1=explode(',',$booked1);
		$first_info2=explode(',',$booked2);
		$first_info3=explode(',',$booked3);
		
		for($m=1,$i=0;$m<=$first_info1[0];$m++)
		{
		   $i+=$first_info1[$m];
		}
		$j=$first_info1[$first_info1[0]+1];
		
		//$i=$first_info1[1]+$first_info1[2]+$first_info1[3];
		//$j=$first_info1[4];
		
		
		
		$seat =array();
		$l = 0;//座位数组的索引
		 for($k = 0; $k < $j; $k++)
		    {
			    $y=chr(65+$k);
			    for($h = 1; $h <= $i; $h++) 
			    {
				
				    $seat[$l]="A".$y."$h";
					$l++;
			
			    }

		}
		//$i=$first_info2[1]+$first_info2[2]+$first_info2[3];
		//$j=$first_info2[4];
		for($m=1,$i=0;$m<=$first_info2[0];$m++)
		{
		   $i+=$first_info2[$m];
		}
		$j=$first_info2[$first_info2[0]+1];
		 for($k = 0; $k < $j; $k++)
		    {
			    $y=chr(65+$k);
			    for($h = 1; $h <= $i; $h++) 
			    {
				
				    $seat[$l]="B".$y."$h";
					$l++;
			
			    }

		}
		//$i=$first_info3[1]+$first_info3[2]+$first_info3[3];
		//$j=$first_info3[4];
		for($m=1,$i=0;$m<=$first_info3[0];$m++)
		{
		   $i+=$first_info3[$m];
		}
		$j=$first_info3[$first_info3[0]+1];
		 for($k = 0; $k < $j; $k++)
		    {
			    $y=chr(65+$k);
			    for($h = 1; $h <= $i; $h++) 
			    {
				
				    $seat[$l]="C".$y."$h";
					$l++;
			
			    }

		}
	    return  $seat;
	}
	
	function shuxue($a,$b)
	{

       return($a-$b);
	 }

	//获取飞机剩余座位信息
	//输入：航班ID
	//输出:以数组输出飞机剩余座位，否则报错
    function getRemainseat($id)
	{
		$ID=$id;
		$a=$this->getBookedseat($ID);
		
		
		$Allseat=$this->getAllseat($ID);
        
		
        $i = count($a);
		$j = count($Allseat);
		
		
        for($k = 0; $k < $i; $k++)//双循环遍历
		{
			for($h = 0; $h < $j; $h++)
			{
				if($Allseat[$h] == $a[$k])
				    unset($Allseat[$h]);
			}
		}
        sort($Allseat);
		
		$result = array();
		$m = 0;
		
		for( $ix = 0; $ix < $j; $ix ++ )
		{
			if( $Allseat[$ix] != null )
			{
				$result[$m]['label'] = $Allseat[$ix];//
				$m ++ ;
			}
		}

		return $result;
        
	} 
	
	//修改飞机信息  
	//输入：飞机id，飞机型号， 
	//返回：修改成功或者失败原因
	function modifyPlane($id,$model,$first_class,$second_class,
						 $third_class,$first_info,$second_info,$third_info)
	{
		if(!empty($id))
		{
		$this->Plane->id = $id;
		$plane = array ('Plane'=>
						array('model'=>$model,
							  'first_class'=>$first_class,
							  'second_class'=>$second_class,
							  'third_class'=>$third_class,
							  'first_info'=>$first_info,
							  'second_info'=>$second_info,
							  'third_info'=>$third_info
							  )
						);
		//测试数据是否保存
		if ($this->Plane->save($plane))
		{
			
		   return true;
		}
	   else 
	   {
		   return false;
	   }
		}
		else
		{
			return 'false1';
		}
		
	}	
}
?>
